// import { call } from 'redux-saga/effects';
import {
    getmyleaverecord,
    getmyvocation,
    getmyattendancedetail,
    getTypelistall,
    getTempInfo
} from '@/services/attendance/attendance';
import { getPopoverList } from '@/services/attendance/vacationBalance';
import { dictionaryList } from '@/services/dictionary';

export default {
    namespace: 'holiday',
    state: {
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        attendancedetail: [],
        typelistall: [],
        leaveStatus: []

    },
    effects: {
        *fetchMyleaverecord({ payload }, { put, call }) {
            const [leaverecord, vocation] = yield [
                call(getmyleaverecord, payload.leaverecordParame),
                call(getmyvocation, payload.vocationParame)
            ];
            if (leaverecord.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        myleaverecord: leaverecord.data.rows,
                        pagination: {
                            current: leaverecord.data.pageNo,
                            pageSize: leaverecord.data.pageSize,
                            total: leaverecord.data.total
                        }
                    }
                });
            }
            if (vocation.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        myvocation: vocation.data
                    }
                });
            }
        },
        *getmyattendancedetail({ payload }, { call, put }) {
            const response = yield call(getmyattendancedetail, payload.parame);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        attendancedetail: response.data.rows
                    }
                });
            }
        },
        *fetchTypelistall({ callback }, { call, put }) {
            const response = yield call(getTypelistall);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        typelistall: response.data
                    }
                });
                callback();
            }
        },
        // 根据假期类型显示对应余额
        *getPopoverList({ payload }, { put, call }) {
            const result = yield call(getPopoverList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        popoverList: result.data
                    }
                });
            }
        },
        /**
         * @param {[string]} codes 字典code集合
         * isShowChildFormat 是否展示树状 1展示 0 不展示,只有 codes 传省市区才有多级，默认不做树状展示
         * 想要省市区树形数据 调用 getCascaderOptions 方法获取
         */
        *fetchDictionaryList({ payload, callback }, { put, call }) {
            const response = yield call(dictionaryList, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        leaveStatus: response.data
                    }
                });
            }
            if (callback)callback()
        },
        // 获取撤销请假模板数据
        *featTempInfo({ payload, callback }, { call }) {
            const result = yield call(getTempInfo, payload);
            if (result.code === 10000) {
                if (callback)callback(result.data)
            }
        }

    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        }
    }
};
